package cn.edu.scau.cmi.huangzhiyuan.composite.safe;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Client extends Application{
	// 根节点
	private static Composite root;
	
	/**
	 * 手动生成节点数据
	 */
	static {
		root = new Composite(Type.ISSUE, "root");
		Composite A = new Composite(Type.ISSUE, "A");
		Leaf B = new Leaf(Type.PERSON, "B");
		root.add(A);
		root.add(B);
		Composite C = new Composite(Type.ISSUE, "C");
		A.add(C);
		Leaf D = new Leaf(Type.PERSON, "D");
		C.add(D);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		
		TreeItem<String> rootItem = new TreeItem<>("Root");
	    rootItem.setExpanded(true);

	    // 添加节点
	    addItems(rootItem, root);
	    
	    // 建树
	    TreeView<String> tree = new TreeView<>(rootItem);
	    
	    // 加入面板
	    StackPane pane = new StackPane();
	    pane.getChildren().add(tree);
	    
	    // 展示
	    primaryStage.setScene(new Scene(pane));
	    primaryStage.show();
	}
	
	/**
	 * 添加当前节点的所有子节点
	 *
	 */
	private void addItems(TreeItem<String> node, Composite root) {
		for(Component item : root.getChildren()) {
			TreeItem<String> nodeChild = new TreeItem<String>(item.getInfo());
			node.getChildren().add(nodeChild);
			
			// 如果当前节点不是叶子，递归添加子节点
			if(item instanceof Composite) {
				addItems(nodeChild, (Composite)item);
			}	
		}
	}
	

	public static void main(String[] args) {
		launch(args);
	}

}
